package com.jxb.dp20251355;

public class MaximumSubarray1_53 {

    public static void main(String[] args) {

    }

    public static int maxSubArray(int[] nums) {
        //状态转移公式:maxSum = max(nums[i],(sum+nums[i]))
        //dp数组：dp[i] = max(nums[i],dp[i-1] + nums[i])
        int[] dp = new int[nums.length];
        //初始化dp数组
        dp[0] = nums[0];
        int result = dp[0];
        for (int i = 1;i< nums.length;i++) {
            dp[i] = Math.max(dp[i-1]+nums[i],nums[i]);
            if (result < dp[i]) {
                result = dp[i];
            }
        }
        return result;
    }

}
